/*
 * @Author: yuqy
 * @Date: 2023-04-11 20:00:33
 * @LastEditors: yuqy
 * @LastEditTime: 2023-10-24 10:49:22
 * @Descripttion: -
 */
import Cookie from 'js-cookie'
import localforage from 'localforage'
/**
 * @description 工具类
 */
/* eslint-disable */
const util = {}
const APP_TOKEN = 'APP_TOKEN'
import { Message } from 'view-ui-plus'
/**
 *  获取token
 *
 * @returns token
 */
util.getToken = () => {
    let token = Cookie.get(APP_TOKEN);
    if (token == 'undefined') token = null;
    return token;
}

/**
 * 设置token
 * @param {*} token token
 * @param {*} expires 过期时间 默认1天
 */
util.setToken = (token, expires = 1) => {
    Cookie.set(APP_TOKEN, token, { expires })
}


/**
 * @name: 时间戳转换
 * @msg: 
 * @return {*}
 */
util.format = (time, format) => {
    const t = new Date(time);
    const tf = function (i) { return (i < 10 ? '0' : '') + i };
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) {
        let timeValue = ''
        switch (a) {
            case 'yyyy':
                timeValue = tf(t.getFullYear());
                break;
            case 'MM':
                timeValue = tf(t.getMonth() + 1);
                break;
            case 'mm':
                timeValue = tf(t.getMinutes());
                break;
            case 'dd':
                timeValue = tf(t.getDate());
                break;
            case 'HH':
                timeValue = tf(t.getHours());
                break;
            case 'ss':
                timeValue = tf(t.getSeconds());
                break;
        }
        return timeValue;
    });
}


/**
 * @name: 获取屏幕倍数比例
 * @msg: 
 * @return {*}
 */
const calcDPpx = () => {
    let dppx = 1
    for (var i = 4; i >= 1; i = (i - 0.05).toFixed(2)) {
        if (window.matchMedia('(-webkit-min-device-pixel-ratio: ' + i + ')').matches || window.matchMedia('(min-resolution: ' + i + 'dppx)').matches) {
            dppx = i
            break
        }
    }
    return dppx
}
/**
 * @name: 获取屏幕宽高，并设置上限制
 * @msg: 
 * @return {*}
 */
util.getWidthHeight = () => {
    let dppx = calcDPpx()
    // let winWidth = window.screen.width * dppx // 屏幕的实际分辨率
    // let winHeight = window.screen.height * dppx
    let winWidth = window.screen.width
    let winHeight = window.screen.height
    let winRealHeight = window.screen.height // 屏幕直接获取到高度
    let winRealWidth = window.screen.width
    let personWidth = winWidth * 0.25 // 个人中心弹框的宽度
    let homeWidth = winWidth * 0.15 // 主讲端页面宽度
    let wrapRight = 80 // 主讲窗口距离右边距离
    let proportion = winWidth / winHeight // 宽高比例
    if (proportion > 2) { // 屏幕超宽了，需要对屏幕宽度进行限制
        winWidth = winHeight / 1080 * 1920
        homeWidth = winWidth * 0.15
        personWidth = winWidth * 0.25
    }
    let localVideoHeight = parseInt(homeWidth / 288 * 108) // 本地预览视频的高度
    let localVideoMinHeight = localVideoHeight * 1.25
    let clientHeight = parseInt(localVideoHeight * 4 * 1.5 + localVideoHeight + localVideoHeight * 0.3) // 窗口高度
    return {
        winWidth,
        winHeight,
        winRealHeight,
        winRealWidth,
        dppx,
        personWidth,
        homeWidth,
        wrapRight,
        localVideoHeight,
        localVideoMinHeight,
        clientHeight
    }
}

// 后台的预约网址
util.copyAdminWebUrl = () => {
    let transfer = document.createElement("input")
    document.body.appendChild(transfer)
    transfer.value = "https://platform.njodin.com"
    transfer.focus()
    transfer.select()
    if (document.execCommand("copy")) {
        document.execCommand("copy")
    }
    transfer.blur()
    document.body.removeChild(transfer)
    Message.success({ content: '成功复制到剪切板，请打开浏览器预约!' });
}

// localforage 主要优点  存储空间大  无限制 而且是异步的
util.getLocalforage = (name = 'myIndexedDB') => {
    const myIndexedDB = localforage.createInstance({
        name,
    })
    return myIndexedDB
}

// 深度clone
util.getDeepObg = (obg) => {
    return JSON.parse(JSON.stringify(obg))
}
export default util
